package common.db;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import lombok.var;

import java.sql.*;
import java.util.LinkedList;
import java.util.List;

public class SqlHelper {
    public static void exceptionIfNoClass(String className) throws ClassNotFoundException {
        Class.forName(className);
    }

    public static Connection getConnection(String url, String user, String password) throws SQLException {
        return DriverManager.getConnection(url, user, password);
    }

    public static void close(Statement statement) throws SQLException {
        statement.getConnection().close();
    }

    public static void close(ResultSet resultSet) throws SQLException {
        close(resultSet.getStatement());
    }

    public static void debug(Connection connection) throws SQLException {
        var meta = connection.getMetaData();
        var v1 = meta.getCatalogSeparator();
        var v2 = meta.getIdentifierQuoteString();
        var v3 = meta.getSQLKeywords();
        connection.close();
    }

    public static String getQuote(Connection connection) throws SQLException {
        var meta = connection.getMetaData();
        var result = meta.getIdentifierQuoteString();
        connection.close();
        return result;
    }

    public static String getDbName(Connection connection) throws SQLException {
        var name = connection.getSchema();
        connection.close();
        return name;
    }

    public static JSONArray getDbInfos(Connection connection) throws SQLException {
        var meta = connection.getMetaData();
        //meta.getCatalogs() 空的
        return getJArray(meta.getSchemas());
    }

    //列序号,从1开始 边界是<=
    public static List<ColumnInfo> getColumns(ResultSet result) throws SQLException {
        var meta = result.getMetaData();
        var list = new LinkedList<ColumnInfo>();
        for (var index = 1; index <= meta.getColumnCount(); index++) {
            var column = new ColumnInfo();
            column.javaType = meta.getColumnClassName(index);
            column.catalog = meta.getCatalogName(index);
            column.name = meta.getColumnName(index);
            column.db = meta.getSchemaName(index);
            column.table = meta.getTableName(index);
            column.sqlType = meta.getColumnTypeName(index);
            column.typeId = meta.getColumnType(index);
            column.label = meta.getColumnLabel(index);
            column.size = meta.getColumnDisplaySize(index);
            column.precision = meta.getPrecision(index);
            column.scale = meta.getScale(index);
            list.add(column);
        }
        return list;
    }

    public static JSONArray getJArray(ResultSet result) throws SQLException {
        var columns = getColumns(result);
        var jArray = new JSONArray();
        while (result.next()) {
            var json = new JSONObject();
            for (var column : columns)
                json.set(column.name, result.getObject(column.name));
            jArray.add(json);
        }
        close(result);
        return jArray;
    }

    public static List<String> getStrings(ResultSet result) throws SQLException {
        var list = new LinkedList<String>();
        while (result.next())
            list.add(result.getString(1));
        close(result);
        return list;
    }
}
